﻿using System;

using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

namespace IFD2
{
    public partial class DepBoarding : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            form1.DefaultButton = btnBoard.ID;
            lblError.Text = "";
            if (!IsPostBack)
            {
                if (Session["ACL"].ToString().IndexOf("DA1") < 0)
                {
                    Response.Redirect("unauthorized.aspx");
                }

                if (Session["FLT_DATE"] != null)
                    txtFltDate.Text = Convert.ToDateTime(Session["FLT_DATE"].ToString()).ToShortDateString();
                if (Session["FLT_NO"] != null)
                    txtFltNo.Text = Session["FLT_NO"].ToString();
                txtAirline.Text = Session["AIRLINE"].ToString();
                lblAirport.Text = Session["DCS_AIRPORT"].ToString();
                txtPassNo.Attributes.Add("onfocus", "javascript:this.select();");
            }
        }

        protected void lbtnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                DateTime.Parse(txtFltDate.Text);
            }
            catch
            {
                lblError.Text = "Invalid date format!";
                return;
            }

            litSound.Text = "";

            int ret_code;
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "bll_flt_get_boarding_count";
            SqlParameter para = cmd.Parameters.AddWithValue("@flt_date", txtFltDate.Text);
            para = cmd.Parameters.AddWithValue("@op_airline", txtAirline.Text);
            para = cmd.Parameters.AddWithValue("@op_flt_no", txtFltNo.Text);
            para = cmd.Parameters.AddWithValue("@airport", Session["DCS_AIRPORT"].ToString());
            para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
            para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
            para = cmd.Parameters.Add("@checked_in", SqlDbType.SmallInt);
            para.Direction = ParameterDirection.Output;
            SqlParameter para2 = cmd.Parameters.Add("@boarded", SqlDbType.SmallInt);
            para2.Direction = ParameterDirection.Output;
            try
            {
                ret_code = TinyDAL.Exec(ref cmd);
                switch (ret_code)
                {
                    case 0:
                        lblCheckedIn.Text = para.Value.ToString();
                        lblBoarded.Text = para2.Value.ToString();
                        break;
                    case 50:
                        lblCheckedIn.Text = "Unauthorized airline!";
                        lblBoarded.Text = "";
                        break;
                    default:
                        lblCheckedIn.Text = "Error:" + ret_code.ToString();
                        break;
                }
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblCheckedIn.Text = ex.Message;
                else
                    lblCheckedIn.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }

        protected void btnBoard_Click(object sender, EventArgs e)
        {
            try
            {
                DateTime.Parse(txtFltDate.Text);
            }
            catch
            {
                lblError.Text = "Invalid date format!";
                //litSound.Text = "";
                litSound.Text = "<embed src='images/sound_stop.wav' loop=false volume='0' hidden='true' autostart=true name=bgss type='audio/mpeg'/>";
                return;
            }

            this.txtPassNo.Focus();
            if (Session["ACL"].ToString().IndexOf("DB6") < 0)
            {
                lblError.Text = "Unauthorized!";
                //litSound.Text = "";
                litSound.Text = "<embed src='images/sound_stop.wav' loop=false volume='0' hidden='true' autostart=true name=bgss type='audio/mpeg'/>";
                return;
            }
            Session["FLT_NO"] = txtFltNo.Text;
            Session["FLT_DATE"] = txtFltDate.Text;
            string[] array;
            array = txtPassNo.Text.Split('/');
            if (array.GetLength(0) != 2)
            {
                lblError.Text = "Bad format!";
                //litSound.Text = "";
                litSound.Text = "<embed src='images/sound_stop.wav' loop=false volume='0' hidden='true' autostart=true name=bgss type='audio/mpeg'/>";
                return;
            }
            if (!Regex.IsMatch(array[0], @"^\d{15}$"))
            {
                lblError.Text = "Bad format!";
                //litSound.Text = "";
                litSound.Text = "<embed src='images/sound_stop.wav' loop=false volume='0' hidden='true' autostart=true name=bgss type='audio/mpeg'/>";
                return;
            }
            if (!Regex.IsMatch(array[1], @"^\d{1}$"))
            {
                lblError.Text = "Bad format!";
                //litSound.Text = "";
                litSound.Text = "<embed src='images/sound_stop.wav' loop=false volume='0' hidden='true' autostart=true name=bgss type='audio/mpeg'/>";
                return;
            }
            string order_no, pax_no, trip_no;
            trip_no = array[1];
            order_no = array[0].Substring(0, 12);
            pax_no = array[0].Substring(12, 3);

            int ret_code;
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "bll_tkt_set_status_at_gate";
            SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", order_no);
            para = cmd.Parameters.AddWithValue("@pax_sn", pax_no);
            para = cmd.Parameters.AddWithValue("@trip_sn", trip_no);
            para = cmd.Parameters.AddWithValue("@flt_date", txtFltDate.Text);
            para = cmd.Parameters.AddWithValue("@op_airline", txtAirline.Text);
            para = cmd.Parameters.AddWithValue("@op_flt_no", txtFltNo.Text);
            para = cmd.Parameters.AddWithValue("@airport", Session["DCS_AIRPORT"].ToString());
            para = cmd.Parameters.AddWithValue("@action", "B");//board
            para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
            para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
            para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
            para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
            para = cmd.Parameters.Add("@pax_name", SqlDbType.VarChar);
            para.Size = 100;
            para.Direction = ParameterDirection.Output;
            SqlParameter para2 = cmd.Parameters.Add("@pax_id", SqlDbType.VarChar);
            para2.Size = 100;
            para2.Direction = ParameterDirection.Output;
            SqlParameter para3 = cmd.Parameters.Add("@seat", SqlDbType.VarChar);
            para3.Size = 10;
            para3.Direction = ParameterDirection.Output;
            try
            {
                ret_code = TinyDAL.Exec(ref cmd);
                switch (ret_code)
                {
                    case 0:
                        lblError.Text = "OK.";
                        lblName.Text = para.Value.ToString();
                        lblIDNo.Text = para2.Value.ToString();
                        lblSeat.Text = para3.Value.ToString();
                        lbtnQuery_Click(sender, e);
                        break;
                    case 3:
                        lblError.Text = "Wrong passenger!";
                        break;
                    case 4:
                        lblError.Text = "Flight not open to board!";
                        break;
                    case 5:
                    case 6:
                        lblError.Text = "Inproper passenger status!" + ret_code.ToString();
                        break;
                    case 8:
                        lblError.Text = "Passenger not found!";
                        break;
                    case 9:
                        lblError.Text = ">> KEAP this passenger please! 请截留该旅客！ <<";
                        break;
                    case 10:
                        lblError.Text = "Board failed!";
                        break;
                    case 50:
                        lblError.Text = "Unauthorized airline!";
                        break;
                    default:
                        lblError.Text = "Error:" + ret_code.ToString();
                        break;
                }


                //Quick Time的值是0（無聲）到100（最大聲），而WMP則是-3400（無聲）到0（最大聲）；
                if (ret_code == 0)
                {
                    litSound.Text = "<embed src='images/sound_pass.wav' loop=false volume='0' hidden='no' autostart=true name=bgss type='audio/wav'/>";
                }
                else
                {
                    litSound.Text = "<embed src='images/sound_stop.wav' loop=false volume='0' hidden='true' autostart=true name=bgss type='audio/mpeg'/>";
                }
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
                //litSound.Text = "";
                //litSound.Text = "<embed src='images/sound_stop.wav' loop=false volume='0' hidden='true' autostart=true name=bgss type='audio/mpeg'/>";
            }
        }
        protected void btnUnboard_Click(object sender, EventArgs e)
        {
            try
            {
                DateTime.Parse(txtFltDate.Text);
            }
            catch
            {
                lblError.Text = "Invalid date format!";
                return;
            }

            litSound.Text = "";
            if (Session["ACL"].ToString().IndexOf("DB6") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            Session["FLT_NO"] = txtFltNo.Text;
            Session["FLT_DATE"] = txtFltDate.Text;
            string[] array;
            array = txtPassNo.Text.Split('/');
            if (array.GetLength(0) != 2)
            {
                lblError.Text = "Bad format!";
                return;
            }
            if (!Regex.IsMatch(array[0], @"^\d{15}$"))
            {
                lblError.Text = "Bad format!";
                return;
            }
            if (!Regex.IsMatch(array[1], @"^\d{1}$"))
            {
                lblError.Text = "Bad format!";
                return;
            }
            string order_no, pax_no, trip_no;
            trip_no = array[1];
            order_no = array[0].Substring(0, 12);
            pax_no = array[0].Substring(12, 3);

            int ret_code;
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "bll_tkt_set_status_at_gate";
            SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", order_no);
            para = cmd.Parameters.AddWithValue("@pax_sn", pax_no);
            para = cmd.Parameters.AddWithValue("@trip_sn", trip_no);
            para = cmd.Parameters.AddWithValue("@flt_date", txtFltDate.Text);
            para = cmd.Parameters.AddWithValue("@op_airline", txtAirline.Text);
            para = cmd.Parameters.AddWithValue("@op_flt_no", txtFltNo.Text);
            para = cmd.Parameters.AddWithValue("@airport", Session["DCS_AIRPORT"].ToString());
            para = cmd.Parameters.AddWithValue("@action", "U");//Unboard
            para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
            para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
            para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
            para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
            para = cmd.Parameters.Add("@pax_name", SqlDbType.VarChar);
            para.Size = 100;
            para.Direction = ParameterDirection.Output;
            SqlParameter para2 = cmd.Parameters.Add("@pax_id", SqlDbType.VarChar);
            para2.Size = 100;
            para2.Direction = ParameterDirection.Output;
            SqlParameter para3 = cmd.Parameters.Add("@seat", SqlDbType.VarChar);
            para3.Size = 10;
            para3.Direction = ParameterDirection.Output;
            try
            {
                ret_code = TinyDAL.Exec(ref cmd);
                switch (ret_code)
                {
                    case 0:
                        lblError.Text = "OK.";
                        lblName.Text = para.Value.ToString();
                        lblIDNo.Text = para2.Value.ToString();
                        lblSeat.Text = para3.Value.ToString();
                        lbtnQuery_Click(sender, e);
                        break;
                    case 3:
                        lblError.Text = "Trip not found!";
                        break;
                    case 4:
                        lblError.Text = "Flight not proper status!";
                        break;
                    case 5:
                    case 6:
                        lblError.Text = "Inproper passenger status!";
                        break;
                    case 8:
                        lblError.Text = "Passenger not found!";
                        break;
                    case 10:
                        lblError.Text = "Action failed!";
                        break;
                    case 50:
                        lblError.Text = "Unauthorized airline!";
                        break;
                    default:
                        lblError.Text = "Error:" + ret_code.ToString();
                        break;
                }

            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }

        
    }
}